package com.gaotong.science.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaotong.science.entity.History;
import com.gaotong.science.entity.HistoryQuery;
import com.gaotong.science.service.HistoryService;
import com.gaotong.science.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suouyang
 * @since 2021-11-02
 */
@RestController
@CrossOrigin
@RequestMapping("/science/history")
public class HistoryController {

    @Autowired
    HistoryService historyService;
    @GetMapping("selectAll")
    public R selectAll(){
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        List<History> historyList = historyService.list(queryWrapper);
        return R.ok().data("data",historyList);
    }

    @PostMapping("selectAllCondition")
    public R selectAllCondition(@RequestBody HistoryQuery historyQuery){
        String beginTime = historyQuery.getBeginTime();
        String endTime = historyQuery.getEndTime();
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(beginTime)){
           queryWrapper.ge("time",beginTime);
//
       }
     if(!StringUtils.isEmpty(endTime)){
         queryWrapper.le("time",endTime);

     }
        List<History> historyList = historyService.list(queryWrapper);
       return R.ok().data("data",historyList);
    }
    @DeleteMapping("deleteByName/{name}")
    public R deleteByName(@PathVariable String name){
        boolean flag = historyService.removeById(name);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }


}

